package com.nondo.rdp.finance.endpoint.api.app;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.auth.endpoint.model.Query;
import com.nondo.rdp.core.common.BeanUtils;
import com.nondo.rdp.core.domain.attach.Attach;
import com.nondo.rdp.core.model.View;
import com.nondo.rdp.finance.domain.product.SecurityOpen;
import com.nondo.rdp.finance.model.SecurityOpenQuery;
import com.nondo.rdp.finance.service.product.SecurityOpenService;

@RestController("apiAppSecurityOpen")
@RequestMapping("/api/app/securityOpen")
public class SecurityOpenEndpoint {
    @Autowired
    SecurityOpenService securityOpenService;
    
    /**
     * 查看证券开户
     * @param request
     * @param response
     * @param id
     * @return
     */
    @RequestMapping("/info/{id}")
	public View<SecurityOpenQuery> info(HttpServletRequest request, HttpServletResponse response,
			@PathVariable("id") Long id) {
		View<SecurityOpenQuery> view = new View<SecurityOpenQuery>(20000, "获取数据成功");
		
		SecurityOpenQuery securityOpenQuery = new SecurityOpenQuery();
		if (id != null) {
			SecurityOpen securityOpen = securityOpenService.findOne(id);
        	BeanUtils.copy(securityOpen, securityOpenQuery);
        	Attach attach = securityOpen.getImage();
        	securityOpenQuery.setImagePath(attach.getPath());
		}

		view.setData(securityOpenQuery);
		return view;
	}

    /**
     * 获取证券开户列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @RequestMapping("/list")
    public View<List<SecurityOpenQuery>> list(HttpServletRequest request, HttpServletResponse response) {
        View<List<SecurityOpenQuery>> view = new View<List<SecurityOpenQuery>>(20000, "获取列表成功");
        
        List<SecurityOpenQuery> securityOpenQueryList = new ArrayList<SecurityOpenQuery>();
        Query<SecurityOpen> query = new Query<SecurityOpen>();
        query.setPage(1);
        query.setSize(10);
        SecurityOpen securityOpenBean = query.getData();
		if (securityOpenBean == null) {
			securityOpenBean = new SecurityOpen();
		}
		securityOpenBean.setIsShow(1);
        
        Page<SecurityOpen> page = securityOpenService.findAll(new Page<SecurityOpen>(query.getPage(), query.getSize()), securityOpenBean);
        
        List<SecurityOpen> securityOpenList = page.getContent();
        
        for(SecurityOpen securityOpen : securityOpenList){
        	SecurityOpenQuery securityOpenQuery = new SecurityOpenQuery();
        	BeanUtils.copy(securityOpen, securityOpenQuery);
        	Attach attach = securityOpen.getImage();
        	securityOpenQuery.setImagePath(attach.getPath());
        	securityOpenQueryList.add(securityOpenQuery);
        }

        view.setData(securityOpenQueryList);

        return view;
    }

}